/**
 * 厦门大学计算机专业 | 华为开发专家(HDE)
 * 专注《零基础学编程系列》  http://lblbc.cn/blog
 * 包含：鸿蒙 | Java | 安卓 | 前端 | Flutter | iOS | 小程序
 * 公众号：蓝不蓝编程
 */
package cn.lblbc.myapplication

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import cn.lblbc.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MyApplicationTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    StopWatchView(modifier = Modifier.padding(innerPadding))
                }
            }
        }
    }
}

@Composable
fun StopWatchView(modifier: Modifier = Modifier) {
    var timeElapsed by remember { mutableStateOf(0L) }
    var isStarted by remember { mutableStateOf(false) }
    var startTime = 0L
    var lastTimeElapsed = 0L
    val interval = 50L
    val handler = Handler(Looper.getMainLooper())

    fun updateTime(){
        if(isStarted){
            timeElapsed = System.currentTimeMillis()-startTime+lastTimeElapsed
            handler.postDelayed({updateTime()},interval)
        }
    }

    fun startTimer(){
        isStarted = true
        startTime = System.currentTimeMillis()
        updateTime()
    }
    fun pauseTimer(){
        isStarted = false
        lastTimeElapsed = timeElapsed
    }
    fun resetTimer(){
        isStarted = false
        timeElapsed = 0L
        lastTimeElapsed = 0L
    }
    Column(
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        Text(text = formatTime(timeElapsed), fontSize = 40.sp)
        Row {
            Button(
                onClick = {
                    if( isStarted){
                        pauseTimer()
                    }else
                    {
                        startTimer()
                    }
                },
                modifier = Modifier
                    .size(100.dp)
                    .padding(10.dp)
            ) {
                Text(text = if(isStarted) "暂停" else "开始")
            }
            Button(
                onClick = {
                    resetTimer()
                },
                modifier = Modifier
                    .size(100.dp)
                    .padding(10.dp)
            ) {
                Text(text = "重置")
            }
        }
    }
}

fun formatTime(timeElapsed: Long): String {
    var seconds = timeElapsed/1000
    var minutes = seconds/60
    seconds %= 60
    var milliSeconds = timeElapsed % 1000
    return String.format("%02d:%02d.%03d",minutes,seconds,milliSeconds)
}

@Preview(showBackground = true)
@Composable
fun StopWatchViewPreview() {
    MyApplicationTheme {
        StopWatchView()
    }
}